package cn.innosmart.aq.util;

import java.util.TimeZone;

/**
 * 解决时区问题的方案
 * Created by David on 2015/8/24.
 */
public class TimeZoneUntil {
    public static char sign = '+';
    public static int hourOffset = 0;
    public static int minOffset = 0;
    public static char offSetSign = '+';
    public static String getTimeZone() {
        TimeZone tz = TimeZone.getDefault();
        String disPlayName = tz.getDisplayName(false, TimeZone.SHORT);
        return disPlayName;
    }
    public static void initTimeZone() {
        TimeZone tz = TimeZone.getDefault();
        String disPlayName = tz.getDisplayName(false, TimeZone.SHORT);
        if (disPlayName.length() > 3) {
            sign = disPlayName.charAt(3);
        } else {
            return;
        }
        //GMT[+|-]hh[[:]mm]   GMT+08:00
        if (sign == '+') {
            offSetSign = '-';
            String[] offset = disPlayName.split("\\+");
            String strOffset = "";
            if (offset.length > 1) {
                strOffset = offset[1];
                hourOffset = Integer.valueOf(strOffset.split(":")[0]);
                minOffset = Integer.valueOf(strOffset.split(":")[1]);
            }
        } else if (sign == '-') {
            offSetSign = '+';
            String[] offset = disPlayName.split("-");
            String strOffset = "";
            if (offset.length > 1) {
                strOffset = offset[1];
                hourOffset = Integer.valueOf(strOffset.split(":")[0]);
                minOffset = Integer.valueOf(strOffset.split(":")[1]);
            }
        }
        toHubTimeLineOffset();
        toAppTimeLineOffset();
    }

    public static int ToHubOffSet = 0;

    public static int toHubTimeLineOffset() {
        ToHubOffSet = 0;
        if (offSetSign == '+') {
            ToHubOffSet = hourOffset * 60 + minOffset;
        } else if (offSetSign == '-') {
            ToHubOffSet = -hourOffset * 60 - minOffset;
        }
        return ToHubOffSet;
    }

    public static int ToAppOffSet = 0;

    public static int toAppTimeLineOffset() {
        ToAppOffSet = 0;
        if (sign == '+') {
            ToAppOffSet = hourOffset * 60 + minOffset;
        } else if (sign == '-') {
            ToAppOffSet = -hourOffset * 60 - minOffset;
        }
        return ToAppOffSet;
    }
}
